package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.CourseEntity;
import org.example.system.entity.TeacherEntity;
import org.example.system.mapper.CourseMapper;
import org.example.system.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 课程服务实现
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 按条件分页查询
     *
     * @param course
     * @return
     */
    @Override
    public IPage<CourseEntity> selectPage(CourseEntity course) {
        MPJLambdaWrapper<CourseEntity> wrapper = new MPJLambdaWrapper<>();
        // 条件
        if (StrUtil.isNotEmpty(course.getName())) {
            wrapper.like(CourseEntity::getName, course.getName());    // 相似的课程名称（模糊查询）
        }
        if (course.getTeacherId() != null) {
            wrapper.eq(CourseEntity::getTeacherId, course.getTeacherId());    // 相同的任课教师工号（精确查询）
        }
        if (StrUtil.isNotEmpty(course.getType())) {
            wrapper.eq(CourseEntity::getType, course.getType());    // 相同的课程类型（精确查询）
        }
        if (course.getStartTime() != null) {
            wrapper.eq(CourseEntity::getStartTime, course.getStartTime());    // 相同的开始时间（精确查询）
        }
        if (course.getEndTime() != null) {
            wrapper.eq(CourseEntity::getEndTime, course.getEndTime());    // 相同的结束时间（精确查询）
        }

        if (StrUtil.isNotEmpty(course.getTeacher())) {
            // 筛选当前时间在开始时间和结束时间之间
            Date now = new Date();
            wrapper.and(w -> w.le(CourseEntity::getStartTime, now).and(w2 -> w2.ge(CourseEntity::getEndTime, now)));
        }

        //联查TeacherEntity
        wrapper.selectAll(CourseEntity.class);
        wrapper.leftJoin(TeacherEntity.class, TeacherEntity::getId, CourseEntity::getTeacherId);
        wrapper.selectAs(TeacherEntity::getUsername, "teacher");

        IPage<CourseEntity> page = new Page<>();
        page.setCurrent(course.getPageNum());
        page.setSize(course.getPageSize());
        return courseMapper.selectPage(page, wrapper);
    }

    /**
     * 添加课程
     *
     * @param course
     * @return
     */
    @Override
    public int add(CourseEntity course) {
        course.setCreateTime(new Date());
        return courseMapper.insert(course);
    }

    /**
     * 修改
     *
     * @param course
     * @return
     */
    @Override
    public int update(CourseEntity course) {
        course.setUpdateTime(new Date());
        return courseMapper.updateById(course);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return courseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     *
     * @param id
     * @return
     */
    @Override
    public CourseEntity getOne(Long id) {
        return courseMapper.selectById(id);
    }
}
